package com.neusoft.liduan.d0922;

public class RunnableHuoChePiao implements Runnable{
	private int i = 100;

//	@Override
//	public synchronized void run() {//同步方法
//		while(i >= 1) {
//			System.out.println(Thread.currentThread() + "卖了第" + i + "张票");
//			i--;
//		}
//	}
	private Object obj = new Object();
	@Override
	public void run() {
		/*	synchronized(同步锁) {
			
			}
		 * 
		 */
//		synchronized(new Object()) {错误写法
//			
//		}
		while(true) {
			synchronized (obj) {
				if(i >= 1) {
					System.out.println(Thread.currentThread() + "卖了第" + i + "张票");
					i--;
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}else {
					break;
				}
			}
		}
		
	}
	
}
